package org.videopractice.common;

import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;

public class FileUtils {

    public static void writeFileToByte(byte[] array) {
        FileOutputStream outputStream = null;

        try {
            outputStream = new FileOutputStream(FileManager.buildOuterFile("codec.h264"), true);
            outputStream.write(array);
            outputStream.write('\n');
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static String writeFileToString(byte[] array) {
        char[] HEX_CHAR_TABLE = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

        StringBuilder stringBuilder = new StringBuilder();

        for(byte b :array) {
            stringBuilder.append(HEX_CHAR_TABLE[(b & 0xF0) >> 4]);
            stringBuilder.append(HEX_CHAR_TABLE[(b & 0x0F)]);
        }

        Logger.d("=============================" + stringBuilder.toString());

        FileWriter fileWriter = null;

        try {
            fileWriter = new FileWriter(FileManager.buildOuterFile("codecH264.txt"), true);
            fileWriter.write(stringBuilder.toString());
            fileWriter.write("\n");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
          if(fileWriter != null) {
              try {
                  fileWriter.close();
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
        }

        return stringBuilder.toString();
    }

} 
